package com.building.hcrm.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseAsyncTask;
import com.building.hcrm.base.BaseStaffSelector.SelectedStaffListener;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.User;
import com.building.hcrm.model.SignLog;
import com.building.hcrm.model.SignParam;
import com.building.hcrm.model.SignQuery;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.ui.customcontrols.ComStaffSelector;
import com.building.hcrm.wcf.CheckInServiceProxy;

public class CheckInListActivity extends BaseActivity
{
	private ComStaffSelector _selector;
	private ListView _lstv;
	private SimpleAdapterCheckList _lstAdapter;
	private List<HashMap<String, Object>> _listHM;
	private ImageButton _imgBtnRe;
	private TextView _txtTitle;
	private ImageButton _imgBtnTitleRight;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_checkin_list);
		InitTitle();
		_selector = (ComStaffSelector) findViewById(R.id.staff_selector);
		_selector.load(ComStaffSelector.DISPLAY_TYPE_ALL, User.getInstance(CheckInListActivity.this));
		_lstv = (ListView) findViewById(R.id.lstCheckIn);
		_selector.setOnSelectedStaffListener(selectedStaffListener);
	}
	
	private void InitTitle()
	{
		_imgBtnRe = (ImageButton) findViewById(R.id.imgbtn_home);
		_imgBtnRe.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO 自动生成的方法存根
				finish();
			}
		});
		_txtTitle = (TextView) findViewById(R.id.titlePageName);
		_txtTitle.setText(R.string.checkin_query_title);
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}
	
	private SelectedStaffListener selectedStaffListener = new SelectedStaffListener()
	{
		@Override
		public void onSelectedStaff(SysUser inStaff)
		{
			if (null != inStaff)
			{
				SignParam param = new SignParam();
				param.userNum = inStaff.getUserNum();
				new GetDataListTask(CheckInListActivity.this).execute(param);
			}
		}
	};
	
	private List<SignQuery> getSignLog(SignParam param) throws Exception
	{
		List<SignQuery> re = null;
		re = CheckInServiceProxy.getInstance(CheckInListActivity.this).GetSignSelfList(param);
		return re;
	}

	private void getReportListAdapter(List<SignQuery> lst)
	{
		try
		{
			if (_listHM == null)
			{
				_listHM = new ArrayList<HashMap<String, Object>>();
			}
			_listHM.clear();

			if(lst!=null)
			{
				for (int i = 0; i < lst.size(); i++)
				{
					SignLog item = lst.get(i);
					HashMap<String, Object> hashMap = new HashMap<String, Object>();
					hashMap.put("strTime", ComFunc.GetStringByDate( item.SignTime,ComConst.DATE_TIME_NOSECOND_FORMAT));
					hashMap.put("strAction", "签到");
					hashMap.put("strPlace", item.SignAddress);
					_listHM.add(hashMap);
				}	
			}
			
			_lstAdapter = new SimpleAdapterCheckList(getApplicationContext(), _listHM, R.layout.list_item_check,
					new String[] { "strTime", "strAction", "strPlace" }, new int[] { R.id.txtCheckTime,
							R.id.txtCheckAction, R.id.txtCheckPlace });

			_lstv.setAdapter(_lstAdapter);
		}
		catch (Exception e)
		{
			Log.e(this.getClass().toString(), ComFunc.getExceptionMessage(e));
		}
	}

	/**
	 * 继承SimpleAdapter重写getView
	 */
	private class SimpleAdapterCheckList extends SimpleAdapter
	{
		public SimpleAdapterCheckList(Context context, List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to)
		{
			super(context, data, resource, from, to);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (super.getCount() < 1)
				return null;
			View v = super.getView(position, convertView, parent);

			return v;
		}
	}

	protected class GetDataListTask extends BaseAsyncTask<Object, Object, List<SignQuery>>
	{
		public GetDataListTask(Context context, String strMsg)
		{
			super(context, strMsg);
		}

		public GetDataListTask(Context context)
		{
			 super(context);
		}

		// 后台操作前被UI thread调用
		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
		}

		@Override
		protected List<SignQuery> doInBackground(Object... params)
		{
			List<SignQuery> obj = null;
			try
			{
				if(null != params && params.length >0 )
				{
					obj = getSignLog((SignParam)params[0]);
				}
			}
			catch (Exception e)
			{
				Log.e(LTAG,ComFunc.getExceptionMessage(e),e);
			}
			return obj;
		}

		@Override
		protected void onPostExecute(List<SignQuery> result)
		{
			super.onPostExecute(result);
			getReportListAdapter(result);
		}
	}

}
